package Runde1;

import java.util.*;

public class LVA {
	
	private final int lvaNr;
	private String title;
	private Date earliestRegistration;
	private Date latestRegistration;
	private Date deregistration;
	private int maxMembers;
	private HashMap<String, Student> students;
	
	/**
	 * creates a new lva
	 * 
	 * @param lvaNr - unique lva number
	 * @param title - lva title
	 * @param earliestRegistration - earliest date to register  
	 * @param latestRegistration - latest date to register
	 * @param deregistration - latest date to deregister
	 * @param maxMembers - maximum amount of members. if unlimited value is integer.MAX_VALUE 
	 * @throws IllegalLVAException - numerous error cases (end of registration precede it's beginning, ...)
	 * 
	 */
	public LVA(int lvaNr, String title, Date earliestRegistration, 
			Date latestRegistration, Date deregistration, int maxMembers) throws IllegalLVAException {
		
		if (latestRegistration.before(earliestRegistration)) {
			throw new IllegalLVAException("End of registration must not precede its beginning");
		} else if (deregistration.before(latestRegistration)) {
			throw new IllegalLVAException("End of deregistration must not precede the end of registration");
		} else if (deregistration.before(earliestRegistration)) {
			throw new IllegalLVAException("End of deregistration must not precede the beginning of registration");
		} else {
			this.lvaNr = lvaNr;
			this.title = title;
			this.earliestRegistration = earliestRegistration;
			this.latestRegistration = latestRegistration;
			this.deregistration = deregistration;
			this.maxMembers = maxMembers;
			this.students = new HashMap<String, Student>();
		}
	}
	
	/**
	 * register a new student to this lva
	 * 
	 * @param s - student to register
	 * @param d - time of registration (for testcases)
	 * @throws CorruptDeRegistrationException - numerous error cases (LVA is already full, student is already registered ...)
	 */
	public void register(Student s, Date d) throws CorruptDeRegistrationException {
		if(d.after(earliestRegistration)&&d.before(latestRegistration)) {
			if(maxMembers>students.size()) {
					if (students.containsKey(s.getMatrNr())) {
						throw new CorruptDeRegistrationException("stundent is already registered");
					} else {
						students.put(s.getMatrNr(), s);
					}
			} else
				throw new CorruptDeRegistrationException("the course is full");
		} else
			throw new CorruptDeRegistrationException("not within registration period");
	}
	
	/**
	 * deregister a student from this lva
	 * 
	 * @param s - student to deregister
	 * @param d - time of deregistration
	 * @throws CorruptDeRegistrationException - end of deregstration has already passed
	 */
	public void deregister(Student s, Date d) throws CorruptDeRegistrationException {
		if(d.before(deregistration))
			students.remove(s.getMatrNr());
		else
			throw new CorruptDeRegistrationException("The end of deregistration has already passed");
	}
	
	/**
	 * @return - list of all registered students
	 */
	public HashMap<String, Student> getStudents() {
		return students;
	}
	
	/**
	 * @return - readable list of all registered students
	 */
	public String getStudentList() {
		String output = "";
		for (Student s: students.values()) {
			output = s.getMatrNr() + " | " + s.getName(); 
		}
		output += "\n";
		return output;
	}
	
	/**
	 * @return - readable output of LVA
	 */
	public String toString() {
		return  "#: " + lvaNr + " title: " + title + "\n\tearliest registration: " +
		earliestRegistration.toString() + "\n\tlatest registration: " + 
		latestRegistration.toString() + "\n\tderegistration until: " + 
		deregistration.toString() + "\n\t# of students: " + students.size() + " of " + maxMembers + "\n";
	}
	

}
